package com.ys.commons.model;

import com.ys.commons.constant.ResponseCode;
import lombok.Data;

/**
 * 通用的响应结果
 *
 * @author zengchao
 * @date 2021-07-03 09:49:52
 */
@Data
public class JsonResponse<T> {
    /**
     * 响应码
     */
    private String code;
    /**
     * 响应信息
     */
    private String msg;
    /**
     * 接口数据
     */
    private T data;

    public static <T> JsonResponse<T> ok(T data){
        JsonResponse<T> response = new JsonResponse<>();
        response.setData(data);
        response.setCode(ResponseCode.OK.getCode());
        response.setMsg(ResponseCode.OK.getMessage());
        return response;
    }

    public static <T> JsonResponse<T> error(String errorMsg,T data){
        JsonResponse<T> response = new JsonResponse<>();
        response.setData(data);
        response.setCode(ResponseCode.ERROR.getCode());
        if (errorMsg == null || "".equals(errorMsg)){
            response.setMsg(ResponseCode.ERROR.getMessage());
        }else {
            response.setMsg(errorMsg);
        }
        return response;
    }
}
